﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.UtilityView.ViewModel;
using EzDesk.DesktopClient.ViewContract;
using EzDesk.DesktopClient.ViewModel;
using EzDesk.MVVMBase;


namespace EzDesk.DesktopClient.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class ShellView : WindowExtenBase, IShellView
    {
        readonly WindowState _windowState;
        NotifyIcon _notifyIcon;

        public ShellView()
        {
            InitializeComponent();
            InitIcon();
            _windowState = WindowState;
        }

        #region IShellView Members

        public IShellViewModel ViewModel
        {
            get
            {
                return DataContext as IShellViewModel;
            }
            set
            {
                DataContext = value;
            }
        }

        #endregion

        private void InitIcon()
        {
            _notifyIcon = new NotifyIcon {Icon = new Icon("NotifyIcon.ico"), Visible = true};
            _notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;

            _notifyIcon.ContextMenu = new ContextMenu(
                new[] {
                     new MenuItem(){ Text = "设置"}
                     ,new MenuItem(){ Text = "退出",}
                }
            );

            //_notifyIcon.ContextMenu.MenuItems[1].Click += ShellView_Click;
            _notifyIcon.ContextMenu.MenuItems[1].Click += ShellView_Click;
            _notifyIcon.ContextMenu.MenuItems[0].Click += Option_Icon_ContextMenu_Click;
        }

        void ShellView_Click(object sender, EventArgs e)
        {
            this.ViewModel.Shutdown();
        }

        private void OnNotifyIconDoubleClick(object sender, EventArgs e)
        {
            Show();
            ShowInTaskbar = true;
            WindowState = _windowState;
            Topmost = true;
        }

        private void Option_Icon_ContextMenu_Click(object sender, EventArgs e)
        {
            OptionPopupEvent.Instance.Publish(new OptionPopupArgs());
        }

        private void ContentControl_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            FunctionNavigateEvent.Instance.Publish(new FunctionNavigateArgs { Url = e.Uri.AbsoluteUri});
        }
    }
}
